using System;
using EngineTest.Components.Cameras;
using EngineTest.Components.Units;
using EngineTest.Components.Vegitation;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Phoenix.Xna.Framework;
using Phoenix.Xna.Framework.Components.Cameras;
using Phoenix.Xna.Framework.Components.Compositors;
using Phoenix.Xna.Framework.Components.Sky;
using Phoenix.Xna.Framework.Components.Terrain;
using Phoenix.Xna.Framework.Graphics;
using Phoenix.Xna.Framework.Input;
using Phoenix.Xna.Framework.Input.Keyboards;
using Phoenix.Xna.Framework.Input.Binding;

namespace EngineTest
{
    public class ExampleGame : Engine
    {
        private Color[] _backgroundColors = new Color[] 
        { 
            Color.Beige, 
            Color.Blue, 
            Color.Green, 
            Color.Gray, 
            Color.Yellow, 
            Color.White, 
            Color.CornflowerBlue 
        };
        private int _colorIndex;
        private ProgressiveTerrain _terrain;
        //private EffectMap _terrainMaterial1;
        //private EffectMap _terrainMaterial2;
        //private Marine _marine;
        //private Texture2D _pixel;
        //private MiniMap _miniMap;
        //private SkyDome _skyDome;

        public ExampleGame()
        {
            InputBindingManager.Instance.Load(@"Settings/InputBindings.xml");
            //SettingManager.Load();
            //BindingManager.Load();
            //Resolution = SettingManager.GetSetting<Vector2>("System", "Resolution");
            //IsFullScreen = SettingManager.GetSetting<bool>("System", "IsFullScreen"); ;
        }

        private void InitializeCompositors()
        {
            Compositors.Add(new ImageCompositor("Logo")
            {
                Position = new Vector2(Resolution.X - 240, Resolution.Y - 75),
                Scale = Vector2.One / 2
            });
            Compositors.Add(new TextCompositor(@"Fonts\Kootenay")
            {
                Position = new Vector2(5, Resolution.Y - 20),
                Text = "Exit : 'Esc' | Toggle WireFrame : 'Tab' | Background Color : 'C'",
                Color = Color.Black
            });
            Compositors.Add(new FramerateCompositor(@"Fonts\Kootenay", true));
            //_miniMap = new MiniMap(@"Terrain\HeightMaps\HeightMap7", @"miniMapUnitMarker")
            //{
            //    Scale = Vector2.One / 4,
            //    Position = new Vector2(5, 5)
            //};
            //Compositors.Add(_miniMap);
        }

        private void InitializeCamera()
        {
            RPGCamera camera = new RPGCamera(ShortGuid.NewGuid().ToString(), 60, 0.5f, 1000000.0f)
            {
                MoveDelta = 50f,
                Position = new Vector3(500, 7000, 500)
            };
            camera.LookAt(new Vector3(1000, 5000, 1000));
            Components.Add(camera);
        }

        private void InitializeTerrain()
        {
            _terrain = new ProgressiveTerrain(@"Terrain\HeightMaps\HeightMap5", 20, new Vector2(64, 1), @"Terrain\EffectMaps\Terrain");
            Components.Add(_terrain);
            //_terrainMaterial1 = _terrain.EffectMap;
            //_terrainMaterial2 = Content.Load<EffectMap>(@"Terrain\EffectMaps\Terrain2");
            //_miniMap.MapScale = _terrain.Scale;
        }

        private void InitializeSkyDome()
        {
            //_skyDome = new SkyDome(this);
            //_skyDome.Initialize();
            //_skyDome.LoadContent();
            //Components.Add(skyDome);
        }

        private void InitializeVegitation()
        {
            //Tree tree = new Tree(this);
            //Random rnd = new Random();

            //for (int i = 0; i < 8000; i++)
            //{
            //    int x = rnd.Next(0, 32768 * 2);
            //    int z = rnd.Next(0, 32768 * 2);
            //    tree.InstanceTransforms.Add(Matrix.CreateTranslation(x, _terrain.GetHeight(x, z), z));
            //}

            //Components.Add(tree);
        }

        private void InitializeModel()
        {
            //_marine = new Marine()
            //    {
            //        Terrain = _terrain,
            //        Position = new Vector3(100, 0, 100),
            //        Camera = Camera.ActiveCamera
            //    };
            //Camera.ActiveCamera.LookAt(_marine.Position);
            //Components.Add(_marine);
            //_miniMap.Components.Add(Color.Yellow, _marine);
        }

        internal void OnExit(object sender, InputMapEventArgs<KeyboardKeyMap> e)
        {
            Exit();
        }

        internal void OnToggleBackgroundColor(object sender, InputMapEventArgs<KeyboardKeyMap> e)
        {
            _colorIndex++;

            if (_colorIndex >= _backgroundColors.Length)
            {
                _colorIndex = 0;
            }

            FillColor = _backgroundColors[_colorIndex];
        }

        internal void OnToggleWireFrame(object sender, InputMapEventArgs<KeyboardKeyMap> e)
        {
            FillMode = (FillMode == FillMode.Solid) ? FillMode.WireFrame : FillMode.Solid;
        }

        internal void OnToggleTerrainEffectMap(object sender, InputMapEventArgs<KeyboardKeyMap> e)
        {
            //_terrain.EffectMap = (string.Compare(_terrain.EffectMap.ID, "Terrain1", true) == 0) ? _terrainMaterial2 : _terrainMaterial1;
        }

        internal void OnToggleAnimation(object sender, InputMapEventArgs<KeyboardKeyMap> e)
        {
            //if (e.InputMap.Contains(Keys.Left))
            //{
            //    _activeAnimation = (_activeAnimation - 1);
            //}
            //else
            //{
            //    _activeAnimation = (_activeAnimation + 1) % _marine.AnimationClips.Count;
            //}

            //if (_activeAnimation < 0)
            //{
            //    _activeAnimation = _marine.AnimationClips.Count - 1;
            //}

            //_animationController.CrossFade(_marine.AnimationClips.Values.ToList()[_activeAnimation], TimeSpan.FromSeconds(0.3f));

        }

        private void BindInput()
        {
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Game", "Exit", InputEvent.Pressed, OnExit);
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Game", "ToggleColor", InputEvent.Pressed, OnToggleBackgroundColor);
            InputBindingManager.Instance.BindInputMap<KeyboardKeyMap>("Game", "ToggleWireframe", InputEvent.Pressed, OnToggleWireFrame);
        }

        #region Engine Members

        protected override void Initialize()
        {
            base.Initialize();
            BindInput();
        }

        protected override void LoadContent()
        {
            InitializeCompositors();
            InitializeCamera();
            InitializeSkyDome();
            InitializeTerrain();
            //(Camera.ActiveCamera as RPGCamera).Terrain = _terrain;
            InitializeVegitation();
            InitializeModel();
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            float step = (float)gameTime.ElapsedGameTime.TotalSeconds;
            //_skyDome.RealTime = false;
            //_skyDome.Theta += 0.1f * step;
            //if (_skyDome.Theta > (float)Math.PI * 2.0f)
            //    _skyDome.Theta = _skyDome.Theta - (float)Math.PI * 2.0f;
            //_skyDome.Update(gameTime);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(FillColor);
            //_skyDome.Draw(gameTime);
            base.Draw(gameTime);
        }

        #endregion
    }
}
